package huang.homework16;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookingDao {
	BaseDao bd=new BaseDao();
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	//数据库关闭
	void close() throws SQLException{
		if(rs!=null){
			rs.close();
		}
		if(ps!=null){
			ps.close();
		}
		if(con!=null){
			con.close();
		}
	}
	//数据库信息插入
	public boolean insert(String name,int nums,float fund) throws SQLException{
		boolean result=false;
		con=bd.getCon();
		String sql="insert into booking (name,nums,fund) values(?,?,?)";
		ps=con.prepareStatement(sql);
		ps.setString(1, name);
		ps.setInt(2, nums);
		ps.setFloat(3, fund);
		int a=ps.executeUpdate();
		close();
		if(a>0){
			result=true;
		}
		return result;
	}
	//查询数据库每桌的订单
	public List<Booking> selectBookingBydeskid(int deskid) throws SQLException{
		con=bd.getCon();
		String sql="select *from Booking where deskid=?";
		ps=con.prepareStatement(sql);
		ps.setInt(1, deskid);
		rs=ps.executeQuery();
		int id=0;
		int deskid1=0;
		String name=null;
		int nums=0;
		float fund=0f;
		String create_time=null;
		String status=null;
		List<Booking> bList=new ArrayList<Booking>();
		while(rs.next()){
			id=rs.getInt("id");
			deskid=rs.getInt("deskid");
			name=rs.getString("name");
			nums=rs.getInt("nums");
			fund=rs.getFloat("fund");
			create_time=rs.getString("create_time");
			status=rs.getString("status");
			Booking booking =new Booking(id,deskid1,name,nums,fund,create_time,status);
			bList.add(booking);
		}
		close();
		return bList;
	}
}
